package com.p2p.front.service.impl;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.p2p.base.model.ValidateCodeSend;
import com.p2p.base.model.ValidateCodeSend.BizTypeEnum;
import com.p2p.base.model.ValidateCodeSend.SendSMSTypeEnum;
import com.p2p.base.model.ValidateCodeSend.SendTypeEnum;
import com.p2p.base.result.ValidateCodeSendResult;
import com.p2p.base.service.ValidateCodeSendService;
import com.p2p.base.util.Const;
import com.p2p.front.dal.dao.UserDao;
import com.p2p.front.dal.model.User;
import com.p2p.front.service.UserRegisterCheckService;
import com.p2p.front.vo.UserCenterResult;
import com.p2p.front.vo.UserRegisterVo;

/**
 * 
 * @author hongbo
 *
 */
@Service("userRegisterCheckService")
public class UserRegisterCheckServiceImpl implements UserRegisterCheckService {

	private final Logger	logger = LoggerFactory.getLogger(this.getClass());

	@Resource private UserDao				userDao;
	
	@Resource private ValidateCodeSendService	validateCodeSendService;
	
	@Override
	public UserCenterResult checkUidForIdCard(String id, String idcard) {
		UserCenterResult result = new UserCenterResult(true,new ArrayList<String>());
		User u = userDao.findById(id);
		if (!StringUtils.equals(u.getIdcard(), idcard)) {
			result.setSuccess(false);
			if (logger.isDebugEnabled()) {
				logger.debug("输入的身份证号与认证的身份证号不一样");
			}
			result.addMsg("输入的身份证号与认证的身份证号不一样");
		}
		return result;
	}

	@Override
	public UserCenterResult checkRetrievePassword(UserRegisterVo userRegisterVo) {
		UserCenterResult result = new UserCenterResult(true,new ArrayList<String>());
		
		Integer findType = userRegisterVo.getFindPwdType();
		Integer pwdType = userRegisterVo.getPwdType();
		String pwd = userRegisterVo.getPassword();
		String repwd = userRegisterVo.getVerificationPassword();
		String identifyCode = userRegisterVo.getIdentifyingCode();
		boolean isLoginPwdType = Integer.valueOf(UserRegisterVo.PwdType.LOGINPWD.getPwdType()).equals(pwdType);
		
		if (!StringUtils.equals(pwd, repwd)) {
			result.setSuccess(false);
			result.addMsg("两次密码不 一致");
		}
		if (result.isSuccess())  {
			
			// 手机找回
			if (Integer.valueOf(UserRegisterVo.FindPwdType.MOBILEFINDTYPE.getFindType()).equals(findType)) {
				String mobile = userRegisterVo.getUmobile();
				ValidateCodeSend validateCodeSend = new ValidateCodeSend();
				validateCodeSend.setSendIdentity(mobile);
				validateCodeSend.setSendType(SendTypeEnum.SMS);
				validateCodeSend.setBizType(BizTypeEnum.MobileBinding);
				ValidateCodeSendResult valResult = validateCodeSendService.check(validateCodeSend, identifyCode);
				
				if (!valResult.isSuccess()) {
					result.setSuccess(false);
					result.addMsg("验证码有误，请重新获取");
				}
				
				if (isLoginPwdType) {
					
				} else {
					/**
					 * 支付密码
					 */
				}
			} else {

				String email = userRegisterVo.getUemail();
				ValidateCodeSend validateCodeSend = new ValidateCodeSend();
				validateCodeSend.setSendIdentity(email);
				validateCodeSend.setSendType(SendTypeEnum.EMAIL);
				validateCodeSend.setBizType(BizTypeEnum.MailBinding);
				ValidateCodeSendResult valResult = validateCodeSendService.check(validateCodeSend, identifyCode);
				
				if (!valResult.isSuccess()) {
					result.setSuccess(false);
					result.addMsg("验证码有误，请重新获取");
				}
				
				if (isLoginPwdType) {
					
				} else {
					
				}
			}
		}
		return result;
	}

	@Override
	public UserCenterResult checkMobileOrEmailExist(String mobile, String email) {
		UserCenterResult result = new UserCenterResult(true, new ArrayList<String>());
		User user = userDao.checkMobileOrEmailExist(mobile, email);
		if (user != null && user.getUid() != null) {
			result.setSuccess(false);
			if (StringUtils.isNotEmpty(mobile)) {
				if (logger.isDebugEnabled()) {
					logger.debug("手机号 " +  mobile + " 已注册，请修改手机号");
				}
				result.addMsg("手机已存在");
			} else if (StringUtils.isNotEmpty(email)) {
				if (logger.isDebugEnabled()) {
					logger.debug("邮箱 " +  email  + "已注册，请修改邮箱");
				}
				result.addMsg("邮箱已存在");
			}
		}
		return result;
	}

	@Override
	public boolean checkUserNameExist(String name) {
		boolean result = true;
		User user = userDao.checkUserNameExist(name);
		if (user != null && StringUtils.isNotEmpty(user.getUid())) {
			result = false;
		}
		return result;
	}

	@Override
	public boolean checkUserIdcardExist(String idcard) {
		boolean result = true;
		User user = userDao.checkUserIdcardExist(idcard);
		if (user != null && StringUtils.isNotEmpty(user.getUid())) {
			result = false;
		}
		return result;
	}

	@Override
	public UserCenterResult checkMobileOrEmailExist(String mobile,
			String email, int codeType) {
		UserCenterResult result = new UserCenterResult(true, new ArrayList<String>());
		User user = userDao.checkMobileOrEmailExist(mobile, email);
		if (user != null && user.getUid() != null &&
				(codeType == SendSMSTypeEnum.TYPE_REGISTER.getIndex() )) {
			result.setSuccess(false);
			if (StringUtils.isNotEmpty(mobile)) {
				if (logger.isDebugEnabled()) {
					logger.debug("手机号 " +  mobile + " 已注册，请修改手机号");
				}
				result.addMsg("手机已存在 *");
			} else if (StringUtils.isNotEmpty(email)) {
				if (logger.isDebugEnabled()) {
					logger.debug("邮箱 " +  email  + "已注册，请修改邮箱");
				}
				result.addMsg("邮箱已存在 *");
			}
		}
		else if(user == null && (codeType==SendSMSTypeEnum.TYPE_GETPWD.getIndex() ||
				codeType==SendSMSTypeEnum.TYPE_MODIFYPHONE.getIndex())){
			result.setSuccess(false);
			if (StringUtils.isNotEmpty(mobile)) {
				if (logger.isDebugEnabled()) {
					logger.debug("手机号 " +  mobile + " 不存在，请修改手机号");
				}
				result.addMsg("手机号码不存在 *");
			} else if (StringUtils.isNotEmpty(email)) {
				if (logger.isDebugEnabled()) {
					logger.debug("邮箱 " +  email  + "不存在，请修改邮箱");
				}
				result.addMsg("邮箱不存在 *");
			}
		} 
		return result;
	}
}